package com.twu.biblioteca.memberdata;

/**
 * Created with IntelliJ IDEA.
 * User: Maximilian
 * Date: 3/10/13
 * Time: 11:52 AM
 * To change this template use File | Settings | File Templates.
 */
class IdManager {

    public static void main (String [] args) throws Exception
    {
        IdManager idManager = new IdManager();
        for(int i = 0; i < 10; i++)
        {
            idManager.getNextId();
            for(int j = 0; j < idManager.idAt.length; j++)
            {
                System.out.print(idManager.idAt[j]);
            }
            System.out.println();
        }
        for(int i = 0; i < idManager.idAt.length; i++)
        {
            System.out.print(idManager.idAt[i]);
        }
        System.out.println();

    }

    int [] idAt = new int[7];

    public IdManager()
    {
        for(int i = 0; i < 7; i++)
        {
            idAt[i] = 1;
        }
    }

    public String getNextId() throws OutOfMemberIdsException
    {
        if(!canIncId())
            throw new OutOfMemberIdsException("No more ids available");
        String id = "";
        for(int i = 0; i < 4; i++)
        {
            id = idAt[i] + id;
        }
        id = "-" + id;
        for(int i = 0; i < 3; i++)
        {
            id = idAt[4+i] + id;
        }
        incIdAt();
        return id;
    }

    private boolean canIncId()
    {
        for(int i = 0; i < idAt.length; i++)
        {
            if(idAt[i] != 9)
                return true;
        }
        return false;
    }

    private void incIdAt()
    {
        int at = 0;
        while(idAt[at] == 9)
        {
            idAt[at] = 1;
            at++;
        }
        idAt[at]++;
    }
}
